<?php
/** 环迅充值
 * Created by PhpStorm.
 * User: KaZeline
 * Date: 15-9-2
 * Time: 上午11:35
 */

header ( 'Content-type:text/html;charset=utf-8' );
error_reporting('E-ALL');
if(!$_SESSION['sid']){exit("<script>window.location='dlogin.html'</script>");}
require_once("/var/www/html/new/conf/le.je");
$le=new go_sql();
$le->connect();
//生成订单号
function uuid() {
    $uid=$_SESSION['sid'];
    $c=7-strlen($uid);
    $f=$c?rand(pow(10,($c-1)),(pow(10,$c)-1)):"";
    return uniqid($f.$uid);
}
$out_trade_no = uuid();
do{
    $has="";
    $out_trade_no=uuid();
    $sel="select id from pft_alipay_rec where out_trade_no='".$out_trade_no."' limit 1";
    $GLOBALS['le']->query($sel);
    $GLOBALS['le']->fetch_assoc();
    $has=$GLOBALS['le']->f("id");
}
while ($has);
$total_fee=($_POST["money"]+0);//单位是元

switch($_SESSION['dtype']){
    case 0:$dt="供应商";break;
    case 1:$dt="分销商";break;
    case 2:$dt="直接供应商";break;
    case 5:$dt="普通用户";break;
}
$subject = '票付通'.$dt.$_SESSION['dname'].'充值';


$uid=$_SESSION['sid'];
//sourceT=3 表示订单是通过环迅支付的
$str=<<<SQL
INSERT INTO pft_alipay_rec SET out_trade_no='$out_trade_no',
seller_email='027619',buyer_email='$uid',
subject='$subject',total_fee='$total_fee',description='',
show_url='',sourceT=3
SQL;

$GLOBALS['le']->query($str);
$GLOBALS['le']->close();
$payDate   = date('Ymd');
$Amount = number_format($total_fee, 2, '.', '');
//IPS证书
$key = '6868423594683207055317415846359181319229560680572746757844258714459288782574783'.
    '5274502434575961846130383064183151730364216831296';
$sign      = 'billno'.'RECHARGE'.$out_trade_no.'currencytypeRMBamount'
    .$Amount.'date'.$payDate.'orderencodetype5'.$key;
$signMd5   = md5($sign);
$Bankco = $_POST['Bankco'];
$payParams = array(
    'Mer_code'  => '027619',
    'Billno'    => 'RECHARGE'.$out_trade_no,
    'Amount'    => $Amount,//单位是元
    'Date'      => $payDate,
    'Currency_Type' => 'RMB',
    'Gateway_Type'  => '01',
    'Lang'          => 'GB',
    'Merchanturl'   => 'http://www.12301.cc/trecord.html',
    'FailUrl'   => '',
    'ErrorUrl'  => '',
    'Attach'    => '帐号余额充值-'.$_SESSION['dname'],//商户数据包
    'DispAmount'    => '',
    'OrderEncodeType'   => 5,//订单支付接口加密方式
    'RetEncodeType' => 17,
    'Rettype'   => 1,
    'ServerUrl' => 'http://www.12301.cc/module/zax/ips_pay/recharge_notify_url.php',
    'SignMD5'   => $signMd5,
    'DoCredit'  => 1,
    'Bankco'    => $Bankco,
);
//    echo '<pre>';
//    print_r($payParams);
//    echo '</pre>';exit;
echo buildPayForm('https://pay.ips.com.cn/ipayment.aspx', $payParams);

function buildPayForm($url, Array $data)
{
    $html = <<<HTML
<html>
  <head>
    <title>跳转......</title>
    <meta http-equiv="content-Type" content="text/html; charset=utf-8" />
  </head>
  <body>
   <form action="{$url}" method="post" id="frm1">
   {inputField}
   </form>
   <script language="javascript">
      document.getElementById("frm1").submit();
    </script>
  </body>
</html>
HTML;
    $inputField = '';
    foreach($data as $key=>$val) {
        $inputField .= "<input type='hidden' name='{$key}' value='{$val}'/>";
    }
    return str_replace('{inputField}', $inputField, $html);
}